package com.nidu.demo.log.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.log.convertor.LoginLogConvertor;
import com.nidu.demo.log.dto.LoginLogCO;
import com.nidu.demo.log.gateway.LoginLogGateway;
import com.nidu.demo.log.model.LoginLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 登录日志单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class LoginLogSingleQryExe {

    private final LoginLogGateway gateway;
    private final LoginLogConvertor convertor;

    public SingleResponse<LoginLogCO> execute(Long id) {
        // 1. 根据ID查询登录日志
        LoginLog loginLog = gateway.getById(id);
        if (loginLog == null) {
            throw new BizException(ErrorCodeConstants.LOGIN_LOG_NOT_FOUND.getCode(), ErrorCodeConstants.LOGIN_LOG_NOT_FOUND.getMessage());
        }

        // 2. 转换为DTO返回
        LoginLogCO loginLogCO = convertor.toClientObject(loginLog);
        return SingleResponse.of(loginLogCO);
    }

}
